package com.lagou.util;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: lishuangqiang
 * @Date: 2019/4/17
 * @Description: 日志工具类
 */

public class LogUtils {


    /** 默认可以打印 */
    private static boolean enable = true;
    /** 默认打印所有级别日志 */
    private static LogLevel minLevel = LogLevel.ALL;
    /** 日期显示格式 */
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");

    public static void setEnable(boolean enable) {
        LogUtils.enable = enable;
    }
    public static void setLogLevel(LogLevel level) {
        LogUtils.minLevel = level;
    }

    public static void info(String msg) {
        finalPrint(LogLevel.INFO, msg);
    }

    public static void warn(String msg) {
        finalPrint(LogLevel.WARN, msg);
    }

    public static void error(String msg) {
        finalPrint(LogLevel.ERROR, msg);
    }

    private static void finalPrint(LogLevel logLevel, String msg) {
        if (!enable) {
            return;
        }
        if (logLevel.isAllow(minLevel)) {
            System.out.printf("%s %s %s\n", formatCurrentTime(), logLevel.getName(), msg);
        }
    }

    private static String formatCurrentTime() {
        return sdf.format(new Date());
    }
}
